var postsData = require("../../../data/posts-data.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var postId = options.id;
    this.data.currentPostId = postId;
    var postData = postsData.postList[postId];
    this.setData({
      "postData": postData
    });

    var postsCollected = wx.getStorageSync("posts_collected");
    if (postsCollected) {
      var postCollected = postsCollected[postId];
      this.setData({
        "collected": postCollected
      });
    } else {
      var postsCollected = {};
      //postsCollected[postId] = false;
      wx.setStorageSync("posts_collected", postsCollected);
    }
  },

  onCollectionTap: function (event) {
    var postsCollected = wx.getStorageSync("posts_collected");
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    wx.setStorageSync("posts_collected", postsCollected);
    this.setData({
      collected: postCollected
    });

    wx.showToast({
      title: postCollected ? '收藏成功' : '取消成功',
      duration: 1000,
      icon: "success"
    })
  },

  onShareTap: function (event) {
    var itemList = [
      '分享给好友',
      '分享到朋友圈',
      '分享到微博'
    ];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: '#405f80',
      success: function (res) {
        console.log(itemList[res.tapIndex]);
      }
    })
  },
  onMusicTap: function (event) {
    var backgroundAudioManager = wx.getBackgroundAudioManager();
    backgroundAudioManager.src = 'https://upload.jxxhy.cn/uploads/mail/20190129/1548769614000.mp3';
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';
    backgroundAudioManager.title = '此时此刻';
    backgroundAudioManager.onError((err) => {
      console.log(err)
    });
  }

})